<?php

include_once '../AtacCommunicator/ACBikesStatus.php';
include_once '../Exceptions/CommunicationException.php';
include_once '../InfoManager/InfoManager.php';
include_once '../Model/BikeSharingStation.php';
include_once '../Model/Location.php';
include_once '../Model/Address.php';
include_once '../Model/GPS.php';

$im = new InfoManager();

switch ($mode=RMobUtils::escape_var($_GET["mode"])) {
	case COMPACT_MODE:
		$mode = EXTENDED_MODE;
		break;
	default:
		$mode = COMPACT_MODE;
}

if ((isset($_GET["lat"])) && (isset($_GET["long"])))
{
	
	$lat = floatval(RMobUtils::escape_var($_GET["lat"]));
	$long = floatval(RMobUtils::escape_var($_GET["long"]));
	$distance = intval(RMobUtils::escape_var($_GET["distance"]));
	
	if ($distance <= 0)
		$distance=15000;
	
	$gps = new GPS($lat,$long);
	$stations = $im->getBikeSharingStationsCloserThan($distance,$gps);
	
} else if (isset($_GET["address"])) {
	$address = RMobUtils::escape_var($_GET["address"]);
	
	$location = new Location ($address);
	
	if ($distance <= 0)
		$distance=15000;
	
	$gps = $location->getGPS();
	$stations = $im->getBikeSharingStationsCloserThan($distance,$gps);
		
}
	else if (isset($_GET["stationName"]))
{
	$station = new BikeSharingStation(RMobUtils::escape_var($_GET["stationName"]));
	$stations = array($station);
		
} 
	else
{

	$stations = $im->getBikeSharingStations();

}

if ($stations != NULL)
{

	$xml = "";
	
	$bikeStatus = new ACBikesStatus($stations);
	
	if ($mode == EXTENDED_MODE) {
		//$stations = $bikeStatus->retrieveBikesStatus();
		$stations = $bikeStatus->retrieveBikesStatus_NEW();
	}
	
	try {
		foreach ($stations as $station)
		{
	
				//Compact mode 
				$xml .= $station->toXML($mode);
		}
	}
	catch (CommunicationException $e)
	{
		
		echo $e->toXML(EXTENDED_MODE);
		exit(1);
	}
}

$xml = "<BIKESHARINGSTATIONS>"."\n".$xml."\n"."</BIKESHARINGSTATIONS>";

echo $xml;
?>
